package com.auxgroup.smarthome.enterpriseapi.responsebody.auxresult;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.*;

/**
 * @Author: laiqiuhua.
 * @Date: 2017/8/18 11:20.
 */
public class AuxAggDataResult extends AbstractAuxErrorResult {
    private AggQuery query;
    private List<AggData> data;

    public static class AggQuery {
        private String start;
        private String end;
        private String attrs;
        private String aggregator;
        private String unit;

        public String getStart() {
            return start;
        }

        public void setStart(String start) {
            this.start = start;
        }

        public String getEnd() {
            return end;
        }

        public void setEnd(String end) {
            this.end = end;
        }

        public String getAttrs() {
            return attrs;
        }

        public void setAttrs(String attrs) {
            this.attrs = attrs;
        }

        public String getAggregator() {
            return aggregator;
        }

        public void setAggregator(String aggregator) {
            this.aggregator = aggregator;
        }

        public String getUnit() {
            return unit;
        }

        public void setUnit(String unit) {
            this.unit = unit;
        }

    }

    public static class AggData {
        private String datetime;
        private JSONObject attrs;

        public String getDatetime() {
            return datetime;
        }

        public void setDatetime(String datetime) {
            this.datetime = datetime;
        }

        public JSONObject getAttrs() {
            return attrs;
        }

        public void setAttrs(JSONObject attrs) {
            this.attrs = attrs;
        }

        /**
         * 仅限于奥克斯电量统计
         * 求某一时间段电量
         * @return
         */
        @JsonIgnore
        public float getElectricity(){
            if (attrs==null)return 0f;
            return attrs.getFloatValue("electricity");
        }
    }

    public AggQuery getQuery() {
        return query;
    }

    public void setQuery(AggQuery query) {
        this.query = query;
    }

    public List<AggData> getData() {
        return data;
    }

    public void setData(List<AggData> data) {
        this.data = data;
    }

    /**
     * 仅限于奥克斯电量统计
     * 求电量总和
     * @return
     */
    @JsonIgnore
    public float getTotalElectricity(){
        if (data == null || data.size() == 0){
            return 0f;
        }
        float total = 0f;
        for(Iterator<AggData> iterator = data.iterator();iterator.hasNext();){
            AggData next = iterator.next();
            total += next.getElectricity();
        }
        return total;
    }

    /**
     * key:可以为时，天  value:对应的耗电总量
     * @return
     */
    @JsonIgnore
    public Map<Integer,Float> getTimeElectricityMap(){
        if ( data == null || data.size() == 0 )return null;
        Map<Integer,Float> timeElectricityMap = new LinkedHashMap<>();
        for(Iterator<AggData> iterator = data.iterator();iterator.hasNext();){
            AggData aggData = iterator.next();
            String datetime = aggData.getDatetime();
            String subTime = datetime.substring(datetime.length() - 2, datetime.length());
            int time = Integer.parseInt(subTime);
            timeElectricityMap.put(time,aggData.getElectricity());
        }
        return timeElectricityMap;
    }


}
